package com.cloudcross.ssp.web.back.main.resource;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.beanutils.PropertyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cloudcross.ssp.base.web.SimpleController;
import com.cloudcross.ssp.common.utils.Pager;
import com.cloudcross.ssp.common.utils.TreeObject;
import com.cloudcross.ssp.model.Resources;
import com.cloudcross.ssp.service.IResourcesService;

/**
 * @author wuqiang.du 2013-11-19
 * @Email: mmm333zzz520@163.com
 * @version 1.0v
 */
@Controller
@RequestMapping("/back/main/resources/")
public class ResourcesController extends SimpleController {
    @Autowired
    private IResourcesService resourcesService;

    @RequestMapping
    public String index() {
        return redirect(page("list"));
    }

    // @RequestMapping("list")
    // public String list(Model model, Resources menu, String pageNow) {
    // return "/back/main/resources/list";
    // }

    @RequestMapping("list")
    public String list(Model model, @RequestParam HashMap<String, Object> paramMap,
                       @RequestParam(defaultValue = "1") int page) {
        int totalRow = 0;
        Pager pager = null;
        pager = new Pager();
        totalRow = resourcesService.countByParams(paramMap);
        pager.setPage(page);
        pager.setTotalRow(totalRow);

        // 循环得到每个客户账号的最后登录时间！

        model.addAttribute("pager", pager);
        model.addAttribute("resourcesList", treeObjects());
        return page("list");
    }

    /**
     * 添加菜单
     * 
     * @param resources
     * @return Map
     */
    @RequestMapping("/create")
    public String create(HttpServletRequest request, Model model) {
        model.addAttribute("resourcesList", treeObjects());
        return page("create");
    }

    /**
     * 编辑菜单
     * 
     * @param resources
     * @return Map
     */
    @RequestMapping("/edit")
    public String edit(HttpServletRequest request, Model model, @RequestParam Long resourceId) {
        List<Resources> resource = resourcesService.findResourcesById(resourceId);
        model.addAttribute("resourceList", resource);
        model.addAttribute("resourcesList", treeObjects());
        return page("edit");
    }

    public List<TreeObject> treeObjects() {
        List<Resources> rs = resourcesService.findAllResources();
        //转换为树对象
        List<TreeObject> treeObjects = new ArrayList<TreeObject>();
        for (Resources res : rs) {
            if (Integer.parseInt(res.getType()) == 0) {
                TreeObject t = new TreeObject();
                try {
                    PropertyUtils.copyProperties(t, res);
                } catch (IllegalAccessException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (InvocationTargetException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (NoSuchMethodException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                treeObjects.add(t);
            }
        }
        for (TreeObject menu : treeObjects) {// 转换为树对象
            List<TreeObject> t = new ArrayList<TreeObject>();
            for (Resources res : rs) {// 转换为树对象
                if (res.getParentId().intValue() == menu.getId().intValue()) {
                    TreeObject s = new TreeObject();
                    try {
                        PropertyUtils.copyProperties(s, res);
                    } catch (IllegalAccessException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    } catch (InvocationTargetException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    } catch (NoSuchMethodException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                    t.add(s);
                }
            }
            for (TreeObject button : t) {
                List<TreeObject> l = new ArrayList<TreeObject>();
                for (Resources res3 : rs) {// 转换为树对象
                    if (res3.getParentId().intValue() == button.getId().intValue()) {
                        TreeObject p = new TreeObject();
                        try {
                            PropertyUtils.copyProperties(p, res3);
                        } catch (IllegalAccessException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        } catch (InvocationTargetException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        } catch (NoSuchMethodException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                        l.add(p);
                    }
                }
                button.setChildren(l);
            }
            menu.setChildren(t);
        }
        return treeObjects;
    }

    @RequestMapping("save")
    public String add(Model model, @ModelAttribute("form") Resources resources) {
        if (resources.getId() == null) {

            try {
                resourcesService.add(resources);
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        } else {
            try {
                resourcesService.update(resources);
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        //resourcesService.add(resources);
        //return redirect(page("list"));
        return redirect("/back/main/resources/list");

    }

    /**
     * 删除资源信息
     * 
     * @param id
     */
    @ResponseBody
    @RequestMapping("delete")
    public void deleteRoles(Integer id) {
        resourcesService.deleteResource(id);
    }

    @ResponseBody
    @RequestMapping("/deleteall")
    public void deleteall(Long[] id) {
        for (int i = 0; i < id.length; i++) {
            resourcesService.deleteResource(Integer.valueOf(id[i].toString()));
        }
    }
}
